/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 涛
 * Date: 2023-09-27
 * Time: 12:43
 */
public class MySingleList implements IList{
    //节点内部类
    static class ListNode {
        public int val;//值
        public ListNode next;//下一个节点

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head;//头结点

    public void createList() {
        ListNode Node1 = new ListNode(12);
        ListNode Node2 = new ListNode(23);
        ListNode Node3 = new ListNode(34);
        ListNode Node4 = new ListNode(45);
        ListNode Node5 = new ListNode(56);

        Node1.next = Node2;
        Node2.next = Node3;
        Node3.next = Node4;
        Node4.next = Node5;

        this.head = Node1;

    }

    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        if(head == null) {
            this.head = node;
        }else {
            node.next = this.head;
            this.head = node;
        }
    }

    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        ListNode cur = this.head;
        if(this.head == null) {
            this.head = node;
        }else {
            //找到尾巴
            while(cur.next != null) {
                cur = cur.next;
            }
            cur.next = node;
        }
    }

    @Override
    public void addIndex(int index, int data) {
        //判断index是否合法
        if(index < 0 || index > size()) {
            //抛一个异常
            return;
        }
        //判断是否为首节点
        if(index == 0) {
            addFirst(data);
        }
        //判断是否为尾结点
        if (index == size()) {
            addLast(data);
        }
        //中间插入节点
        ListNode cur = searchPrev(index);//找index下标位置的节点
        ListNode note = new ListNode(data);//要插入的节点

        note.next = cur.next;
        cur.next = note;
    }
    //查找index下标的节点
    private ListNode searchPrev(int index) {
        ListNode cur = this.head;
        int count = 0;
        while(count != index - 1) {
            cur = cur.next;
            count++;
        }
        return cur;
    }

    @Override
    public boolean contains(int key) {
        ListNode cur = this.head;
        while(cur != null) {
            if(cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {

    }

    @Override
    public void removeAllKey(int key) {

    }

    @Override
    public int size() {
        int count = 0;//计数
        ListNode node = this.head;

        while(node != null)
        {
            count++;
            node = node.next;
        }
        return count;

    }

    @Override
    public void clear() {

    }

    @Override
    public void display() {
        ListNode node = this.head;
        while(node != null) {
            System.out.println(node.val+" ");
            node = node.next;
        }
        System.out.println();
    }

}
